package com.codeoss.framework.view.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Restrictions;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.metadata.TableData;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.util.LookupInfo;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.clientscript.SearchDisplayClientScript;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Search;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.datasource.IDataSource;
import com.codeoss.framework.view.datasource.store.IDataSourceStore;
import com.codeoss.framework.view.service.IVariableService;
import com.codeoss.framework.view.util.DataSourceUtil;

@Service
public class SearchComponentService extends ComponentService {
	@Autowired
	private IVariableService variableService;
	@Autowired
	private ITableStore tableStore;
	@Autowired
	private IDataSourceStore dataSourceStore;
	@Autowired
	private DataSourceUtil dataSourceUtil;
	@Autowired
	private IConverterStore converterStore;
	@Override
	public void processValue(YunContext yunContext, Component component, Map<String, String> paraMap,
			Map<String, Object> dataMap) {
		// TODO Auto-generated method stub
		Search search = (Search) component;
		if (paraMap.containsKey(search.getId())) {
			String value = paraMap.get(search.getId());
			String dataType=search.getDataType();
			if (!StringUtils.isEmpty(value)) {
				variableService.setVariableValue(yunContext, search.getVarName(), converterStore.getConverter(dataType).convertStringToObject(value));
			} else {
				variableService.setVariableValue(yunContext, search.getVarName(), null);
			}
		}
	}
	
	@Override
	public void processEvent(YunContext yunContext, Component component, String eventType) {
		super.processEvent(yunContext, component, eventType);
		Search search = (Search) component;
		if("searchValueDisplayEvent".equals(eventType)) {
			DataSourceConfig dataSourceConfig=dataSourceUtil.getDataSourceConfigById(yunContext.getPage(), search.getDataSource());
			Table table=tableStore.get(dataSourceConfig.getTableName());
			Column column=table.getColumnByColumnName(search.getDataField());
			LookupInfo lookupInfo=column.getLookupInfo();
			Query query=new Query();
			Long value=Long.parseLong(yunContext.getViewRequest().getData().get("value"));
			query.add(Restrictions.eq(lookupInfo.getValueColumn(), value));
			Table lookupInfoTable=tableStore.get(lookupInfo.getDataSourceConfig().getTableName());
			IDataSource dataSource=dataSourceStore.getDataSource(lookupInfoTable.getAccessType());
			TableData tableData=dataSource.select(yunContext, lookupInfoTable, query);
			Map<String,Object> map=(Map<String,Object>)tableData.getData().get(0);
			String displayValue=(String)map.get(lookupInfo.getDisplayColumn());
			SearchDisplayClientScript searchDisplayClientScript=new SearchDisplayClientScript();
			searchDisplayClientScript.setComponentId(yunContext.getViewRequest().getEventSource());
			searchDisplayClientScript.setValue(value);
			searchDisplayClientScript.setDisplay(displayValue);
			yunContext.addClientScript(searchDisplayClientScript);
			yunContext.setRenderCompleted(true);
		}else if ("changeEvent".equals(eventType)) {
			Expression onChangeExpression = search.getOnChangeExpression();
			if (onChangeExpression != null) {
				executeEventExpression(yunContext, component, onChangeExpression,null);
			}
		}
	}
}
